import struct
from socket import *

ADDR = ('0.0.0.0', 8888)
fmt = struct.Struct('i32sif')
if __name__ == "__main__":
    sock_fd = socket(AF_INET, SOCK_DGRAM)
    sock_fd.bind(ADDR)
    with open('学生信息.txt', mode='a+') as file:
        while True:
            data, client_from = sock_fd.recvfrom(1024)
            if not data:
                break
            origin_data_tuple = fmt.unpack(data)
            # print(origin_data_tuple[0], origin_data_tuple[1].decode(), origin_data_tuple[2], origin_data_tuple[3])
            write_data = '%d %s %d %f \n' % (
                origin_data_tuple[0], origin_data_tuple[1].decode(), origin_data_tuple[2], origin_data_tuple[3])
            print(write_data)
            file.write(write_data)
            file.flush()
        sock_fd.close()
